const http = require('../../../utils/http.js')
const Base64 = require('../../../utils/base64.js')

Page({

    /**
     * 页面的初始数据
     */
    data: {
        imgUrl: http.imgUrl,
        inputArr: [
          
        ],
        pwdTrue: true,
        canClick: true,
        loginType: '1'
    },


    /**
     *  获取所有INPUT的数据
     */
    inputTxt(e) {
      // console.log(e.detail.value)
        let inputArr = this.data.inputArr
      // console.log(inputArr)
        let temp = http.inputArr(inputArr, e)
        // console.log(temp)
        this.setData({
            inputArr: temp
        })
    },

    /**
     * 跳过
     */
    ignore() {
        wx.switchTab({
            url: '/pages/index/index'
        })
    },

    /**
     * 返回
     */
    goBack() {
        // wx.navigateBack({
        //     delta: 1
        // })
        wx.navigateTo({
          url: '/pages/chooseIdentity',
        })
    },

    /**
     * 到注册
     */
    goReg() {
        let type = this.data.id
        wx.navigateTo({
            url: '/pages/login/register/register?type=' + type
        })
    },

    /**
     * 到忘记密码
     */
    goForget() {
        wx.navigateTo({
            url: '/pages/login/forget/forget',
        })
    },

    /**
     * 切换验证码登录
     */
    changeLoginType() {
        let that = this

        if (that.data.loginType === '1') {
            that.setData({
                loginType: '2'
            })
        } else {
            that.setData({
                loginType: '1'
            })
        }
    },

    /**
     * 获取短信验证码
     */
    getVercoe() {
        console.log('验证啊')
        let that = this,
            url = 'User_getMessage',
            tel = that.data.inputArr[0]
            // console.lof(tel)
        if (!http.ifEmpty(tel)) {
            wx.showToast({
                title: '请填写手机号',
                icon: 'none',
                duration: 3000
            })
            return false
        }

        if (!http.rexFn(tel)[0]) {
            wx.showToast({
                title: '请输入正确的手机号',
                icon: 'none',
                duration: 3000
            })
            return false
        }

        // console.log(Base64.CusBASE64.encoder('UNO' + tel + 'HACHA'))
        http._post(
            url,
            { mobile: Base64.CusBASE64.encoder('UNO' + tel + 'HACHA'), type: 33},
            res => {
                // console.log(res.data)
                let data = res.data
                if (data.re === '1') {
                    wx.showToast({
                        title: data.info,
                        icon: 'none',
                        duration: 3000
                    })
                    that.setData({
                        ifVerify: true,
                        timeDown: 60
                    })
                    let time = 60,
                        timeCount = setInterval(function () {
                        time--
                        that.setData({
                            timeDown: time
                        })
                        if (time <= 0) {
                            that.setData({
                                ifVerify: false
                            })
                            clearInterval(timeCount)
                        }
                    }, 1000)
                } else {
                    wx.showToast({
                        title: data.info,
                        icon: 'none',
                        duration: 3000
                    })
                }
            },
            err => {
                console.log(err)
            }
        )
    },

    /**
     * 登录
     */
    getLogin() {
        let that = this,
        url = 'User_login',
        inputArr = that.data.inputArr,
        mobile = Base64.CusBASE64.encoder('UNO' + inputArr[0] + 'HACHA'),
        password = inputArr[1] ? Base64.CusBASE64.encoder('UNO' + inputArr[1] + 'HACHA') : "",
        code = inputArr[2] ?inputArr[2]:''
        console.log(code)
        console.log(password)
        console.log(mobile)
        console.log(inputArr.length)
        console.log(inputArr[0])
        if (that.data.loginType === '1') {
     
          if (inputArr.length === 0) {
              wx.showToast({
                title: '请输入手机号',
                icon: "none",
                duration: 3000
              })
          } 
          else if (!password) {
            wx.showToast({
              title: '请输入密码',
              icon: "none",
              duration: 3000
            })
          }
          else{
            http._post(
              url,
              { mobile: mobile, password: password, login_type: 1},
              res => {
                // console.log(res.data)
                let data = res.data
                if (data.re === '1') {
                  wx.setStorageSync('token', data.data.token)
                  wx.showToast({
                    title: data.info,
                    icon: 'none',
                    duration: 3000,
                    success: function () {
                      setTimeout(() => {
                        wx.reLaunch({
                          url: '/pages/chooseIdentity',
                        })
                      }, 500)
                    }
                  })
                  that.setData({
                    canClick: true
                  })
                }
                else {
                  that.setData({
                    errTips: data.info
                  })
                }
              },
              err => {
                console.log(err)
              }
            )
          }

            
        } else {
          if (inputArr.length === 0) {
            wx.showToast({
              title: '请输入手机号',
              icon: "none",
              duration: 3000
            })
          } else if(!code){
              wx.showToast({
                title: '请输入密码',
                icon:"none",
                duration:3000
              })
          }else
          {
            http._post(
              url,
              { mobile: mobile, code: code, login_type: 1 },
              res => {
                // console.log(res.data)
                let data = res.data
                if (data.re === '1') {
                  wx.setStorageSync('token', data.data.token)
                  wx.showToast({
                    title: data.info,
                    icon: 'none',
                    duration: 3000,
                    success: function () {
                      setTimeout(() => {
                        wx.reLaunch({
                          url: '/pages/chooseIdentity',
                        })
                      }, 500)
                    }
                  })
                  that.setData({
                    canClick: false
                  })
                } else {
                  that.setData({
                    errTips: data.info
                  })
                }
              },
              err => {
                console.log(err)
              }
            )
          }
            
        }
    },
    
    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {
   
    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function () {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function () {

    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide: function () {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload: function () {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh: function () {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function () {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage: function () {

    }
})